import threading
from conf.conf_read import ConfRead
from core.exec_cmd import remote_exec
from core.send_file import remote_put


class InfoBar(object):
    def __init__(self):
        self.rconf = ConfRead()

    def group_info(self):
        result = self.rconf.show_group()
        for r1, r2 in result.items():
            print('------ %s ------' % r1)
            for r3, r4 in r2.items():
                print("%s: %s" %(r3, r4))

        while True:
            print('1. 选择主机组')
            print('0. 返回上一级')
            choice = input("please input your choice：")
            if choice == '1':
                group_name = input('输入组名：')
                while True:
                    print('1. exec cmd')
                    print('2. send file')
                    print('0. 返回上一级')
                    choice2 = input("please input your choice: ")
                    group_info = self.rconf.about_group(group_name)
                    if choice2 == '1':
                        cmd = input(">> ")
                        self.exec_cmd(group_info, cmd)

                    elif choice2 == '2':
                        local_file = input("输入本地文件：")
                        remote_file = input("放入到远程目录<需要加上文件名>")
                        self.send_file(group_info, local_file, remote_file)

                    else:
                        break
            else:
                break

    def host_info(self):
        result = self.rconf.show_all_host()
        print('---------- 所有主机 ----------')
        for h1, h2 in result.items():
            print('%s: %s' %(h1, h2))
        while True:
            print('1. 选择主机')
            print('0. 返回上一级')
            choice = input("please input your choice：")
            if choice == '1':
                host_name = input('输入主机名 <空格分开>：')
                while True:
                    print('1. exec cmd')
                    print('2. send file')
                    print('0. 返回上一级')
                    choice2 = input("please input your choice: ")
                    hosts = list(set(host_name.split(' ')))
                    host_info = self.rconf.about_host(hosts)
                    if choice2 == '1':
                        cmd = input(">> ")
                        self.exec_cmd(host_info, cmd)

                    elif choice2 == '2':
                        local_file = input("输入本地文件：")
                        remote_file = input("放入到远程目录<需要加上文件名>：")
                        self.send_file(host_info, local_file, remote_file)

                    else:
                        break

            else:
                break

    def exec_cmd(self, host_list, cmd):
        if len(host_list) > 0:
            t_obj = []
            for i in host_list:
                t = threading.Thread(target=remote_exec, args=(i[0], i[1], i[2], i[3], cmd))
                t.start()
                t_obj.append(t)

            for t in t_obj:
                t.join()
        else:
            print('没有主机')

    def send_file(self, host_list, local_file, remote_file):
        if len(host_list) > 0:
            t_obj = []
            for i in host_list:
                t = threading.Thread(target=remote_put, args=(i[0], i[1], i[2], i[3], local_file, remote_file))
                t.start()
                t_obj.append(t)

            for t in t_obj:
                t.join()
        else:
            print('没有主机')